// pages/myActivity/myActivity.js
import {
    activityAllActivities,
    activityGetLeaveInfo,
    activityGetMyActivities,
    activitySignOut
} from "../../services/myActivityReq";

const {
    getMyActivity,
    activitySignInByActivityId,
    activityAllWaitingForStart
} = require('../../services/myActivityReq');
const moment = require("../../utils/moment")

function formatTime(time) {
    const res = new Date(time);
    return moment(res).format('YYYY-MM-DD hh:mm:ss');
}

Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        pageNum: 1,
        pageSize: 10,
        userId: null,
        familyInfo: [],
        MemberId: null,
        show: false,
        signOutShow: false,
        targetActivityId: null,
        active: 0,
        activiytyLeaveReason: '',
        Reasonshow: false,
        isFinish: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let familyInfo = this.data.familyInfo
        let volunteerInfo = wx.getStorageSync("volunteerInfo")
        familyInfo = wx.getStorageSync("familyInfo").member;
        if (!volunteerInfo) return wx.showToast({
            title: '你还没有登陆',
            icon: "error",
            duration: 2000,
            success(res) {
                setTimeout(() => wx.navigateTo({
                    url: '../login/login',
                }), 2000)
            }
        })
        if (!familyInfo) familyInfo = [volunteerInfo];
        this.handleGetMyActivity()
        let userId = this.data.userId;
        userId = wx.getStorageSync('volunteerInfo').volunteerId;
        this.setData({
            userId,
            familyInfo
        })
    },
    handleGetMyActivity() {
        wx.showLoading({
            title: '加载中……'
        })
        activityAllWaitingForStart(res => {
            console.log(res)
            let list = res.data.data;
            console.log(list)
            this.setData({
                list
            })
            wx.hideLoading()
        })
    },

    // 发布活动风采
    handleAddContent(e) {
        const id = e.detail
        wx.navigateTo({
            url: '../contentAdd/contentAdd?id=' + id,
        })
    },
    handleActivityLeave(e) {
        const id = e.detail

        wx.navigateTo({
            url: '../activityLeave/activityLeave?id=' + id,
        })
    },
    handleGoSign(event) {
        let volunteerId = this.data.MemberId;
        let targetActivityId = this.data.targetActivityId;

        // let volunteerId = this.data.userId;
        wx.showLoading({
            title: "正在加载中"
        })
        wx.scanCode({
            onlyFromCamera: true,
            success: (res) => {
                wx.hideLoading()
                targetActivityId = res.result.split('/').pop()
                if (!volunteerId) return wx.showToast({
                    title: "请先选择签到用户",
                    icon: "error"
                });
                activitySignInByActivityId(targetActivityId, volunteerId, res => {
                    // console.log(res)
                    if (res.data.code !== 0) return wx.showToast({
                        title: res.data.msg,
                        icon: "error"
                    })
                    return wx.showToast({
                        title: "签到成功"
                    })
                })
            },
            fail: (res) => {
                wx.showToast({
                    title: "扫描失败",
                    icon: "error",
                    duration: 2000,
                });
            },

        });
    },
    handleGoSignOut(event) {
        let volunteerId = this.data.MemberId;
        let targetActivityId = this.data.targetActivityId;

        // let volunteerId = this.data.userId;
        wx.showLoading({
            title: "正在加载中"
        })
        wx.scanCode({
            onlyFromCamera: true,
            success: (res) => {
                wx.hideLoading()
                targetActivityId = res.result.split('/').pop()
                if (!volunteerId) return wx.showToast({
                    title: "请先选择签到用户",
                    icon: "error"
                });
                activitySignOut(targetActivityId, volunteerId, res => {
                    // console.log(res)
                    if (res.data.code !== 0) return wx.showToast({
                        title: res.data.msg,
                        icon: "error"
                    })
                    return wx.showToast({
                        title: "签退成功"
                    })
                })
            },
            fail: (res) => {
                wx.showToast({
                    title: "扫描失败",
                    icon: "error",
                    duration: 2000,
                });
            },

        });
    },
    onClose() {
        this.setData({
            show: false,
            signOutShow: false,
        })
    },
    handleActivityAllWaitingForStart() {
        wx.showLoading({
            title: '加载中',
        })
        let list = this.data.list;
        activityAllWaitingForStart(res => {
            wx.hideLoading()
            list = res.data.data;
            this.setData({
                list
            })
        })
    },
    handleActivityALl() {
        let list = this.data.list;
        wx.showLoading({
            title: '加载中',
        })
        activityAllActivities(res => {
            console.log(res)
            list = res.data.data;
            wx.hideLoading()
            this.setData({
                list
            })
        })
    },
    handleGoActivityLeave(event) {
        wx.navigateTo({
            url: '../activityLeave/activityLeave/?id=' + event.currentTarget.dataset.id,
        })
    },
    handleGoAddActivity() {
        wx.navigateTo({
            url: '../activityAsk/activityAsk',
        })
    },
    handleSelectMember(event) {

        let MemberId = this.data.MemberId;
        MemberId = event.currentTarget.dataset.id;
        console.log(MemberId)
        this.setData({
            MemberId
        })

    },
    handleToSignInShow() {
        this.setData({
            show: true
        })
    },
    handleToSignOutShow() {
        this.setData({
            signOutShow: true
        })

    },
    handleGetCreateMyActivity() {
        let params = {
            pageNum: 1,
            pageSize: 999
        }
        wx.showLoading({
            title: '加载中……',
        })
        activityGetMyActivities(params, res => {
            let list = res.data.data.list;
            console.log(list)
            if (list.length < this.data.pageSize) {
                this.setData({
                    isFinish: true
                })
            }

            list.forEach((v, i) => {
                v.activityDatetime = formatTime(new Date(v.activityDatetime)).substring(0, 10)
                try {
                    v.activityAddress = JSON.parse(v.activityAddress).address
                } catch (e) {

                }
            })
            this.setData({
                list
            })
            wx.hideLoading()
        })
    },
    handleSelectTab(e) {
        this.setData({
            list: []
        })
        switch (e.detail.index) {
            case 0:
                this.handleActivityAllWaitingForStart()
                break;
            case 1:
                this.handleActivityALl()
                break;
            case 2:
                this.handleGetCreateMyActivity()
                break;
        }
    },
    handleGetReason(e) {
        this.setData({
            activiytyLeaveReason: e.currentTarget.dataset.reason,
            Reasonshow: true
        })

    },
    handleGetNotLeaveReason(e) {
        // console.log(e.currentTarget.dataset.id)
        console.log(e)
        activityGetLeaveInfo(e.detail, res => {
            // console.log(res)
            if (res.data.code !== 0) return wx.showToast({
                title: '出错啦！！',
                icon: "error"
            })
            this.setData({
                Reasonshow: true,
                activiytyLeaveReason: res.data.data.reason
            })
        })
    }

})